/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version

	@import "poodle"
*/

Poodle_BBCode = function()
{
	if (arguments[0]) {
		initControl(arguments[0]);
	} else {
		init();
	}

	// make tab content divs resizable
	function init()
	{
		var n, i=-1, nodes = document.$Q('textarea.bbcode');
		while (n=nodes[++i]) { initControl(n); }
	}

	function initControl(node)
	{
		if (!defined(node.bbcode)) {
			var b = node.bbcode = $C('div',{'class':'bbcode',textarea:node}), div, i, j, t=Poodle_BBCode.toolbars, URL, fn, a;

			b.help = function(btn) {
				var code = btn.className.replace(/^.*bbcode-([^\s]+).*$/,'$1');
				this.helpDiv.txt(Poodle_BBCode.help[code]);
			};

			b.code = function(btn)
			{
				var code = btn.className.replace(/^.*bbcode-([^\s]+).*$/,'$1'),
				   ToAdd = "["+code+"]", force=false;
				switch (code)
				{
				case 'ltr':
				case 'rtl':
					return this.textarea.dir = code;

				case 'hr':
					return this.write('', "[hr]", true);

				case 'url':
					URL = prompt("Enter the URL", "http://");
					if (URL !== null) {
						if (!URL) { return alert("Error: You didn't write the URL "); }
						if (!this.write("[url="+URL+"]", "[/url]")) {
							var TITLE = prompt("Enter the page name", "Web Page Name");
							if (TITLE !== null) {
								var Add = "]"+URL;
								if (TITLE) { Add = "="+URL+"]"+TITLE; }
								this.write('', "[url"+Add+"[/url]", true);
							}
						}
					}
					return;

				case 'flash':
				case 'video':
					URL = prompt("Enter the "+code+" file URL", "http://");
					if (URL !== null) {
						if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
						var WS = prompt("Enter the "+type+" width", "250");
						if (WS !== null) {
							if (!WS) { WS = 250; }
							var HS = prompt("Enter the "+type+" height", "200");
							if (HS !== null) {
								if (!HS) { HS = 200; }
								this.write('', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
							}
						}
					}
					return;

				case 'email':
				case 'image':
					if (code == 'img') { URL = prompt("Please enter image URL","http://"); }
					else { URL = prompt("Enter the Email Address",""); }
					if (URL !== null) {
						if (!URL) { return alert("Error : You didn't write the Address"); }
						this.write('', "["+code+"]"+URL+"[/"+code+"]", true);
					}
					return;

				case 'color':
				case 'size':
					force = true;
					ToAdd = "["+code+"="+btn.value+"]";
					break;

				case 'justify':
				case 'left':
				case 'center':
				case 'right':
					ToAdd = "[align="+code+"]";
					code  = 'align';
					break;

				default:
					break;
				}
				if (!this.write(ToAdd, "[/"+code+"]", force)) {
					if (!btn.hasClass('active')) {
						btn.addClass('active');
					} else {
						ToAdd = "[/"+code+"]";
						btn.removeClass('active');
					}
					this.write('', ToAdd, true);
				}
			};

			b.write = function(start, end, force)
			{
				var n = this.textarea;
				if (n.caretPos) {
					n.focus();
					// Attempt to create a text range (IE).
					var theSelection = document.selection.createRange().text;
					if (force || theSelection != '') {
						document.selection.createRange().text = start + theSelection + end;
						n.focus();
						return true;
					}
				} else if (typeof(n.selectionStart) != "undefined") {
					// Mozilla text range replace.
					var text = new Array();
					text[0] = n.value.substr(0, n.selectionStart);
					text[1] = n.value.substr(n.selectionStart, n.selectionEnd-n.selectionStart);
					text[2] = n.value.substr(n.selectionEnd);
					var caretPos = n.selectionEnd+start.length+end.length;
					if (force || text[1] != '') {
						n.value = text[0]+start+text[1]+end+text[2];
						if (n.setSelectionRange) {
							n.focus();
							n.setSelectionRange(caretPos, caretPos);
						}
						return true;
					}
				} else if (force) {
					// Just put it on the end.
					n.value += start+end;
					n.focus(textarea.value.length-1);
					return true;
				}
				return false;
			};

			for (i=0; i<t.length; ++i)
			{
				div = b.$A('div');
				for (j=0; j<t[i].length; ++j)
				{
					if (t[i][j]) {
						a = t[i][j][1];
						fn = is_function(t[i][j][2]) ? t[i][j][2] : function(){b.code(this);};
						if (is_array(a)) {
							var x=0, s=div.$A('select',{
								'class':'bbcode-'+t[i][j][0],
								onchange:fn,
								onmouseover:function(){b.help(this);},
							});
							for (;x<a.length;++x) {
								s.$A("option",{textContent:a[x]});
							}
						} else {
							div.$A('button',{
								'class':'bbcode-'+t[i][j][0],
								type:'button',
								textarea:node,
								onclick:fn,
								onmouseover:function(){b.help(this);}
							});
						}
					}
				}
			}
			b.helpDiv = b.$A('div',{textContent:'Tip: Styles can be applied quickly to selected text'});
			b.before(node);
			b.$A(node);

			function storeCaret()
			{
				if (this.createTextRange) this.caretPos = document.selection.createRange().duplicate();
			}
			node.bind('change',storeCaret)
				.bind('keyup',storeCaret)
				.bind('click',storeCaret)
				.bind('select',storeCaret);
		}
	}
};

Poodle_BBCode.help = {
	b: "Bold: [b]bold[/b]",
	i: "Italic: [i]italic[/i]",
	quote: "Quote: [quote]Quoted Text[/quote]",
	code: "Code: [code]Code[/code]",
	php: "PHP: [php]PHP Code[/php]",
	img: "Image: [img]http://image path[/img]",
	color: "Font Color: [color=red]text[/color] You can use HTML color=#FF0000",
	size: "Font Size: [size=9]Very Small[/size]",
	rtl: "Right to Left: Make message box align from Right to Left",
	ltr: "Left to Right: Make message box align from Left to Right",
	email: "Email: [email]Email Here[/email]",
	url: "URL: [url=Page URL]Page name[/url]",
	right: "Right: [align=right]Right Aligned Text[/align]",
	left: "Left: [align=left]Left Aligned Text[/align]",
	center: "Center: [align=center]Center Aligned Text[/align]",
	justify: "Justify: [align=justify]Justified Text[/align]",
	hr: "Horizontal Rule: Horizontal Rule [hr]",
	video:"Insert video file: [video width=# height=#]file URL[/video]",
	flash:"Insert flash file: [flash width=# height=#]flash URL[/flash]"
};

Poodle_BBCode.toolbars = [
	[
		['b'],
		['i'],
		null,
		['justify'],
		['left'],
		['center'],
		['right'],
		null,
		 // Font color
		['color',['','maroon','red','red','orange','brown','yellow','green','olive','cyan','blue','darkblue','indigo','violet','white','black']],
		// Font size
		['size',['',7,9,12,18,24]]
	],[
		['url'],
		['email'],
		['img'],
		['video'],
		null,
		['quote'],
		['code'],
		['php'],
		null,
		['hr'],
		null,
		['ltr'],
		['rtl']
	]
];

// attach to window onload event
Poodle.onDOMReady(function(){Poodle_BBCode();});
